﻿using System.Linq;
using System.Reflection;

namespace UnitTesting.Fixturing
{
	class RecursiveGuard
	{
		readonly ConstructorInfo _constructor;

		internal RecursiveGuard(ConstructorInfo constructor)
		{
			_constructor = Ensure.IsNotNull(() => constructor);
		}

		internal virtual void Do()
		{
			var constructorType = _constructor.DeclaringType;
			var parameterTypes = _constructor.GetParameters().Select(x => x.ParameterType);
			if (parameterTypes.Any(x => x == constructorType))
			{
				throw new RecursiveValueException(constructorType);
			}
		}
	}
}